class GradientColor {
    gradientColor({ startColor, endColor, step, min }) {
        // 转换为rgb数组模式
        let startRGB = this.colorRgb(startColor);
        let endRGB = this.colorRgb(endColor);
        // 总差值
        let sR = (endRGB[0] - startRGB[0]) / step;
        let sG = (endRGB[1] - startRGB[1]) / step;
        let sB = (endRGB[2] - startRGB[2]) / step;
        let salpha = 1 / step;
        let colorArr = [];
        for (let i = 0; i < step; i++) {
            // 计算每一步的hex值
            let hex = this.colorHex(
                'rgba(' +
                    parseInt(sR * i + startRGB[0], 0) +
                    ',' +
                    parseInt(sG * i + startRGB[1], 0) +
                    ',' +
                    parseInt(sB * i + startRGB[2], 0) +
                    ',' +
                    (salpha * i + 0) +
                    ')'
            );
            colorArr.push({ color: hex, value: parseInt(min + i, 0) });
        }
        if (colorArr.length > 100) {
            let multiple = Math.ceil(colorArr.length / 100);
            colorArr = this.handleColor(colorArr, multiple);
        }
        return colorArr;
    }
    handleColor(colorArr, multiple) {
        let arr = [];
        for (let i = 0; i < colorArr.length; i = i + multiple) {
            let value = 0;
            if (i + multiple <= colorArr.length - 1) {
                value = parseInt(
                    (colorArr[i].value + colorArr[i + multiple].value) / 2,
                    0
                );
            }
            arr.push({ color: colorArr[i].color, value });
        }
        console.log(arr);
        return arr;
    }
    // 将hex表示方式转换为rgb表示方式(这里返回rgb数组模式)
    colorRgb(sColor) {
        let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
        sColor = sColor.toLowerCase();
        if (sColor && reg.test(sColor)) {
            if (sColor.length === 4) {
                let sColorNew = '#';
                for (let i = 1; i < 4; i += 1) {
                    sColorNew += sColor
                        .slice(i, i + 1)
                        .concat(sColor.slice(i, i + 1));
                }
                sColor = sColorNew;
            }
            // 处理六位的颜色值
            let sColorChange = [];
            for (let i = 1; i < 7; i += 2) {
                sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2), 0));
            }
            return sColorChange;
        } else {
            let str = sColor.replace(/[^0-9]/gi, ' ');
            let sColorChange = str.trim().split(/\s+/);
            sColorChange = sColorChange.map(item => {
                return parseInt(item, 0);
            });
            return sColorChange;
        }
    }
    // 将rgb表示方式转换为hex表示方式
    colorHex(rgb) {
        let _this = rgb;
        let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
        if (/^(rgb|RGB)/.test(_this)) {
            let aColor = _this.replace(/(?:(|)|rgb|RGB)*/g, '').split(',');
            let strHex = '#';
            for (let i = 0; i < aColor.length; i++) {
                let hex = Number(aColor[i]).toString(16);
                hex = hex < 10 ? String(0) + hex : hex; // 保证每个rgb的值为2位
                if (hex === '0') {
                    hex += hex;
                }
                strHex += hex;
            }
            if (strHex.length !== 7) {
                strHex = _this;
            }
            return strHex;
        } else if (reg.test(_this)) {
            let aNum = _this.replace(/#/, '').split('');
            if (aNum.length === 6) {
                return _this;
            } else if (aNum.length === 3) {
                let numHex = '#';
                for (let i = 0; i < aNum.length; i += 1) {
                    numHex += aNum[i] + aNum[i];
                }
                return numHex;
            }
        } else {
            return _this;
        }
    }
}
export default GradientColor;
